//
//  SANetworking.m
//  Saas
//
//  Created by 杨荣 on 2020/3/12.
//  Copyright © 2020 bing. All rights reserved.
//

#import "SANetworking.h"
#import "SANetManager.h"
#import "SASalesOrderViewListModel.h"


@interface SANetworking()

@end

@implementation SANetworking

static SANetworking *networking;
+ (SANetworking *)shareInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        networking = [[SANetworking alloc] init];
    });
    return networking;
}

- (void)getRequestByFindingProductWithAttrId:(NSString *)attrId
                                 barCodeSign:(BOOL)barCodeSign
                                  customerId:(NSString *)customerId
                            productCodeOrBar:(NSString *)productCodeOrBar
                                successBlock:(NetworkingSuccessBlock)successBlock
                                   failBlock:(NetworkingFailBlock)failBlock {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    if (attrId) {
        [parameters setValue:attrId forKey:@"attrId"];
    }
    if (barCodeSign) {
        [parameters setValue:@"true" forKey:@"barCodeSign"];
    }else {
        [parameters setValue:@"false" forKey:@"barCodeSign"];
    }
    if (customerId) {
        [parameters setValue:customerId forKey:@"customerId"];
    }
    if (productCodeOrBar) {
        [parameters setValue:productCodeOrBar forKey:@"productCodeOrBar"];
    }
    [SANetManager requestWithType:(SkyHttpRequestTypeGet) urlString:SA_BASE_URL(@"/warehouse/purchase/findProduct") parameters:parameters successBlock:^(id responseObject) {
        if (successBlock) {
            successBlock(responseObject);
        }
    } failureBlock:^(NSString *error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
}

- (void)getRequestByGetProductTypeWithSuccessBlock:(NetworkingSuccessBlock)successBlock
                                         failBlock:(NetworkingFailBlock)failBlock {
    [SANetManager requestWithType:(SkyHttpRequestTypeGet) urlString:SA_BASE_URL(@"/product/getConditionV2") parameters:nil successBlock:^(id responseObject) {
        if (successBlock) {
            successBlock(responseObject);
        }
    } failureBlock:^(NSString *error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
}

- (void)postRequestByUpdateCheckWithStockCheckFlowId:(NSString *)stockCheckFlowId
                                     productSkuForms:(NSArray <SASalesOrderViewListModel *>*)productSkuForms
                                              remark:(NSString *)remark
                                    totalBeforeStock:(NSString *)totalBeforeStock
                                     totalAfterStock:(NSString *)totalAfterStock
                                        successBlock:(NetworkingSuccessBlock)successBlock
                                           failBlock:(NetworkingFailBlock)failBlock {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    if (stockCheckFlowId) {
        [parameters setValue:stockCheckFlowId forKey:@"stockCheckFlowId"];
    }
    NSMutableArray *productSkuFormsArray = [NSMutableArray array];
    for (SASalesOrderViewListModel *product in productSkuForms) {
        NSMutableDictionary *productDic = [NSMutableDictionary dictionary];
        if (product.productId) {
            [productDic setValue:product.productId forKey:@"productId"];
        }
        if (product.productCode) {
            [productDic setValue:product.productCode forKey:@"productCode"];
        }
        if (product.productName) {
            [productDic setValue:product.productName forKey:@"productName"];
        }
        NSMutableArray *skuArray = [NSMutableArray array];
        for (SASalesOrderToPendSkuList *sku in product.toPendSkuAttrs) {
            NSDictionary *skucDic = [NSMutableDictionary dictionary];
            if (sku.skuId) {
                [skucDic setValue:sku.skuId forKey:@"skuId"];
            }
            if (sku.count) {
                [skucDic setValue:sku.count forKey:@"beforeStock"];
            }
            if (sku.stock) {
                [skucDic setValue:sku.stock forKey:@"afterStock"];
            }
            if (sku.count && sku.stock) {
                [skucDic setValue:[NSString stringWithFormat:@"%zd",[sku.stock integerValue] - [sku.count integerValue]] forKey:@"diffStock"];
            }
            [skuArray addObject:skucDic];
        }
        if (skuArray.count > 0) {
            [productDic setValue:skuArray forKey:@"skuForms"];
        }
        [productSkuFormsArray addObject:productDic];
    }
    if (productSkuFormsArray.count > 0) {
        [parameters setValue:productSkuFormsArray forKey:@"productSkuForms"];
    }
    if (remark) {
        [parameters setValue:remark forKey:@"remark"];
    }
    if (totalBeforeStock) {
        [parameters setValue:totalBeforeStock forKey:@"totalBeforeStock"];
    }
    if (totalAfterStock) {
        [parameters setValue:totalAfterStock forKey:@"totalAfterStock"];
    }
    if (totalBeforeStock && totalAfterStock) {
        [parameters setValue:[NSString stringWithFormat:@"%zd",[totalAfterStock integerValue] - [totalBeforeStock integerValue]] forKey:@"totalDiffStock"];
    }
    [SANetManager requestWithType:(SkyHttpRequestTypePost) urlString:SA_BASE_URL(@"/stock/check/saveOrUpdate") parameters:parameters successBlock:^(id responseObject) {
        if (successBlock) {
            successBlock(responseObject);
        }
    } failureBlock:^(NSString *error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
}

- (void)getRequestByGetCheckBillsListWithCheckOrderNo:(NSString *)checkOrderNo
                                          checkStatus:(NSString *)checkStatus
                                            startTime:(NSString *)startTime
                                              endTime:(NSString *)endTime
                                          currentPage:(NSInteger)currentPage
                                             pageSize:(NSInteger)pageSize
                                         successBlock:(NetworkingSuccessBlock)successBlock
                                            failBlock:(NetworkingFailBlock)failBlock {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    if (checkOrderNo) {
        [parameters setValue:checkOrderNo forKey:@"checkOrderNo"];
    }
    if (checkStatus) {
        [parameters setValue:checkStatus forKey:@"checkStatus"];
    }
    if (startTime.length > 8) {
        [parameters setValue:startTime forKey:@"startTime"];
    }
    if (endTime.length > 8) {
        [parameters setValue:endTime forKey:@"endTime"];
    }
    [parameters setValue:[NSNumber numberWithInteger:currentPage] forKey:@"currentPage"];
    [parameters setValue:[NSNumber numberWithInteger:pageSize] forKey:@"pageSize"];
    
    [SANetManager requestWithType:(SkyHttpRequestTypeGet) urlString:SA_BASE_URL(@"/stock/invoice/list") parameters:parameters successBlock:^(id responseObject) {
        if (successBlock) {
            successBlock(responseObject);
        }
    } failureBlock:^(NSString *error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
}
- (void)getRequestByGetCheckBillsDetailWithStockCheckFlowId:(NSString *)stockCheckFlowId
                                               successBlock:(NetworkingSuccessBlock)successBlock
                                                  failBlock:(NetworkingFailBlock)failBlock {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    if (stockCheckFlowId) {
        [parameters setValue:stockCheckFlowId forKey:@"stockCheckFlowId"];
    }
    [SANetManager requestWithType:(SkyHttpRequestTypeGet) urlString:SA_BASE_URL(@"/stock/invoice/detail") parameters:parameters successBlock:^(id responseObject) {
        if (successBlock) {
            successBlock(responseObject);
        }
    } failureBlock:^(NSString *error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
}
- (void)postRequestByCheckHandleWithStockCheckFlowIds:(NSString *)stockCheckFlowIds
                                        successBlock:(NetworkingSuccessBlock)successBlock
                                           failBlock:(NetworkingFailBlock)failBlock {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    if (stockCheckFlowIds) {
        [parameters setValue:stockCheckFlowIds forKey:@"stockCheckFlowIds"];
    }
    [SANetManager requestWithType:(SkyHttpRequestTypePost) urlString:SA_BASE_URL(@"/stock/check/handle") parameters:parameters successBlock:^(id responseObject) {
        if (successBlock) {
            successBlock(responseObject);
        }
    } failureBlock:^(NSString *error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
}
- (void)postRequestByCheckClearZeroWithStockCheckFlowIds:(NSString *)stockCheckFlowIds
                                           successBlock:(NetworkingSuccessBlock)successBlock
                                              failBlock:(NetworkingFailBlock)failBlock {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    if (stockCheckFlowIds) {
        [parameters setValue:stockCheckFlowIds forKey:@"stockCheckFlowIds"];
    }
    [SANetManager requestWithType:(SkyHttpRequestTypePost) urlString:SA_BASE_URL(@"/stock/check/clearingZero") parameters:parameters successBlock:^(id responseObject) {
        if (successBlock) {
            successBlock(responseObject);
        }
    } failureBlock:^(NSString *error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
}
- (void)postRequestByCheckCancelWithStockCheckFlowId:(NSString *)stockCheckFlowId
                                        successBlock:(NetworkingSuccessBlock)successBlock
                                           failBlock:(NetworkingFailBlock)failBlock {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    if (stockCheckFlowId) {
        [parameters setValue:stockCheckFlowId forKey:@"stockCheckFlowId"];
    }
    [SANetManager requestWithType:(SkyHttpRequestTypePost) urlString:SA_BASE_URL(@"/stock/check/remove") parameters:parameters successBlock:^(id responseObject) {
        if (successBlock) {
            successBlock(responseObject);
        }
    } failureBlock:^(NSString *error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
}
- (void)getRequestByGetProfitLossListWithStartTime:(NSString *)startTime
                                           endTime:(NSString *)endTime
                                       currentPage:(NSInteger)currentPage
                                          pageSize:(NSInteger)pageSize
                                      successBlock:(NetworkingSuccessBlock)successBlock
                                         failBlock:(NetworkingFailBlock)failBlock {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    if (startTime.length > 8) {
        [parameters setValue:startTime forKey:@"startTime"];
    }
    if (endTime.length > 8) {
        [parameters setValue:endTime forKey:@"endTime"];
    }
    [parameters setValue:[NSNumber numberWithInteger:currentPage] forKey:@"currentPage"];
    [parameters setValue:[NSNumber numberWithInteger:pageSize] forKey:@"pageSize"];
    [SANetManager requestWithType:(SkyHttpRequestTypeGet) urlString:SA_BASE_URL(@"/stock/profitLoss/list") parameters:parameters successBlock:^(id responseObject) {
        if (successBlock) {
            successBlock(responseObject);
        }
    } failureBlock:^(NSString *error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
}
- (void)getRequestByGetProfitLossDetailWithCheckOrderId:(NSString *)checkOrderId
                                      productNameOrCode:(NSString *)productNameOrCode
                                               showDiff:(BOOL)showDiff
                                            currentPage:(NSInteger)currentPage
                                               pageSize:(NSInteger)pageSize
                                           successBlock:(NetworkingSuccessBlock)successBlock
                                              failBlock:(NetworkingFailBlock)failBlock {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    if (checkOrderId) {
        [parameters setValue:checkOrderId forKey:@"checkOrderId"];
    }
    if (productNameOrCode) {
        [parameters setValue:productNameOrCode forKey:@"productNameOrCode"];
    }
    if (showDiff) {
        [parameters setValue:@1 forKey:@"showDiff"];
    }else {
        [parameters setValue:@0 forKey:@"showDiff"];
    }
    [parameters setValue:[NSNumber numberWithInteger:currentPage] forKey:@"currentPage"];
    [parameters setValue:[NSNumber numberWithInteger:pageSize] forKey:@"pageSize"];
    [SANetManager requestWithType:(SkyHttpRequestTypeGet) urlString:SA_BASE_URL(@"/stock/profitLoss/detail") parameters:parameters successBlock:^(id responseObject) {
        if (successBlock) {
            successBlock(responseObject);
        }
    } failureBlock:^(NSString *error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
}
- (void)getRequestByGetNotCheckListWithProductNameOrCode:(NSString *)productNameOrCode
                                       productCategoryId:(NSString *)productCategoryId
                                               startTime:(NSString *)startTime
                                                 endTime:(NSString *)endTime
                                             currentPage:(NSInteger)currentPage
                                                pageSize:(NSInteger)pageSize
                                            successBlock:(NetworkingSuccessBlock)successBlock
                                               failBlock:(NetworkingFailBlock)failBlock {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    if (productNameOrCode) {
        [parameters setValue:productNameOrCode forKey:@"productNameOrCode"];
    }
    if (productCategoryId) {
        [parameters setValue:productCategoryId forKey:@"productCategoryId"];
    }
    if (startTime.length > 8) {
        [parameters setValue:startTime forKey:@"startTime"];
    }
    if (endTime.length > 8) {
        [parameters setValue:endTime forKey:@"endTime"];
    }
    [parameters setValue:[NSNumber numberWithInteger:currentPage] forKey:@"currentPage"];
    [parameters setValue:[NSNumber numberWithInteger:pageSize] forKey:@"pageSize"];
    [SANetManager requestWithType:(SkyHttpRequestTypeGet) urlString:SA_BASE_URL(@"/stock/not/list") parameters:parameters successBlock:^(id responseObject) {
        if (successBlock) {
            successBlock(responseObject);
        }
    } failureBlock:^(NSString *error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
}
@end
